package com.baosight.sczx.zm.ch.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanCM;
import com.baosight.sczx.zm.ch.domain.ZMCHMineValidateData;

/**
 * 
 * <p>Title: ServiceZMCH71.java</p>
 * <p>Description: 生产进度旬验收（采煤）</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年8月17日 上午11:16:35  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCH71 extends ServiceEPBase {
	String fdCheckKey;
	String fdYear;
	String fdMonth;

	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始进入方法</p>
	 * <p>return value: ServiceZMCH71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年8月17日上午11:16:12 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		fdCheckKey = inInfo.getString("keyId");
		fdYear = inInfo.getString("year");
		fdMonth = inInfo.getString("month");
		
		EiInfo outInfo = this.query(inInfo);
		outInfo.setCell("result_add", 0, "fdCheckKey", fdCheckKey);
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询方法</p>
	 * <p>return value: ServiceZMCH71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年8月17日上午11:15:59 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdCheckKey", fdCheckKey, true);
		inInfo.setCell("inqu_status", 0, "fdCheckKey", fdCheckKey);
		EiInfo outInfo = super.query(inInfo, "ZMCHMineValidateData.query", new ZMCHMineValidateData());
		String year = inInfo.getString("year");
		if(year == null || year.trim().equals("")) {
			year = StaticTools.getYear();
		}
		String month = inInfo.getString("month");
		if(month == null || month.trim().equals("")) {
			month = StaticTools.getMonth();
		}
		
		outInfo.addBlock(ProducePlanCM.getWorkingFaceBlock("cmes.ch.workingface", UserSession.getInSessionProperty("miningCode").toString(), year, month, null, true));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true, UserSession.getInSessionProperty("miningCode").toString()));
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: 新增方法</p>
	 * <p>return value: ServiceZMCH71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年8月17日上午11:15:46 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCHMineValidateData _dom = new ZMCHMineValidateData();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));	
			_dom.setFdCheckKey(fdCheckKey);
			
			boolean checIsReport = this.validateIsReport(_dom.getFdCheckKey());
			if(!checIsReport) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("当月的生产计划旬汇报已经上报，不能新增！");
				return out;
			}

			Map<String, String> params = new HashMap<String, String>();

			boolean validate = validate(inInfo, _dom, params);
			if (validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_MINEVALIDATEDATA"));
			this.dao.insert("ZMCHMineValidateData.insert", _dom);
			
			
			Map<String, String> params2 = new HashMap<String, String>();
			params2.put("fdCheckKey", fdCheckKey);
			params2.put("fdYear", fdYear);
			params2.put("fdMonth", fdMonth);
			params2.put("fdProjectId", _dom.getFdProjectId());	// 计算推进累计、面积累计、产量累计用
			params2.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString()); 
			this.dao.update("ZMCHMineValidateData.updateSum",params2);	// 更新推进累计、面积累计、产量累计

			// 更新生产进度旬汇报主表中关于采煤的统计信息
			this.dao.update("ZMCHProduceReport.updateProduceReportMSum",_dom);	
			this.dao.update("ZMCHProduceReport.updateProduceReportMSumM",params2);
			this.dao.update("ZMCHProduceReport.updateProduceReportMSumY",params2);
			this.dao.update("ZMCHProduceReport.updateProduceReportHbb",params2);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: 修改方法</p>
	 * <p>return value: ServiceZMCH71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年8月17日上午11:15:10 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCHMineValidateData _dom = new ZMCHMineValidateData();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			boolean checIsReport = this.validateIsReport(_dom.getFdCheckKey());
			if(!checIsReport) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("当月的生产计划旬汇报已经上报，不能修改！");
				return out;
			}
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			boolean validate = validate(inInfo, _dom, params);
			if (validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			
			this.dao.update("ZMCHMineValidateData.update", _dom);
			
			Map<String, String> params2 = new HashMap<String, String>();
			params2.put("fdCheckKey", fdCheckKey);
			params2.put("fdYear", fdYear);
			params2.put("fdMonth", fdMonth);
			params2.put("fdProjectId", _dom.getFdProjectId());	// 计算推进累计、面积累计、产量累计用
			params2.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			this.dao.update("ZMCHMineValidateData.updateSum",params2);	// 更新推进累计、面积累计、产量累计
			
			// 更新生产进度旬汇报主表中关于采煤的统计信息
			this.dao.update("ZMCHProduceReport.updateProduceReportMSum",_dom);
			this.dao.update("ZMCHProduceReport.updateProduceReportMSumM",params2);
			this.dao.update("ZMCHProduceReport.updateProduceReportMSumY",params2);
			this.dao.update("ZMCHProduceReport.updateProduceReportHbb",params2);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;

	}

	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: 删除方法</p>
	 * <p>return value: ServiceZMCH71</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年8月17日上午11:14:58 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {

		EiInfo outInfo = null;

		for (int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++) {
			
			boolean checIsReport = this.validateIsReport(inInfo.getCellStr(EiConstant.resultBlock, 0, "fdCheckKey"));
			if(!checIsReport) {
				outInfo = this.query(inInfo);
				outInfo.setStatus(-1);
				outInfo.setMsg("当月的生产计划旬汇报已经上报，不能删除！");
				return outInfo;
			}
			
			EiInfo _temp = super.delete(inInfo, "ZMCHMineValidateData.delete");
						
			Map<String, String> params2 = new HashMap<String, String>();
			params2.put("fdCheckKey", fdCheckKey);
			params2.put("fdYear", fdYear);
			params2.put("fdMonth", fdMonth);
			params2.put("fdProjectId", inInfo.getCellStr(EiConstant.resultBlock, i, "dProjectId"));	// 计算推进累计、面积累计、产量累计用
			params2.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString()); 
			
			this.dao.update("ZMCHMineValidateData.updateSum", params2);	// 更新推进累计、面积累计、产量累计
			this.update(inInfo,"ZMCHProduceReport.updateProduceReportMSum");
			this.dao.update("ZMCHProduceReport.updateProduceReportMSumM",params2);
			this.dao.update("ZMCHProduceReport.updateProduceReportMSumY",params2);
			this.dao.update("ZMCHProduceReport.updateProduceReportHbb",params2);
			
			outInfo = this.query(inInfo);
			outInfo.setMsg(_temp.getMsg());
			outInfo.setDetailMsg(_temp.getDetailMsg());
		}

		return outInfo;
	}

	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description: 校验是否重复</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年8月17日上午11:14:44 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCHMineValidateData _dom, Map<String, String> params) {
		if (params == null) {
			params = new HashMap<String, String>();
		}

		String fdCheckKey = _dom.getFdCheckKey();
		String fdProjectId = _dom.getFdProjectId();
		params.put("fdCheckKey", fdCheckKey);
		params.put("fdProjectId", fdProjectId);
		
		List result = dao.query("ZMCHMineValidateData.validate", params);
		
		if (result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "生产进度旬汇报每旬每工作面只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * <p>Function: validateIsReport</p>
	 * <p>Description: 校验生产计划旬汇报是否已上报</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月6日上午9:54:29 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private  boolean validateIsReport(String fdKeyId) {
		if(fdKeyId == null|fdKeyId.trim().equals(""))
			return true;
		
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("fdKeyId", fdKeyId);
		_map.put("fdStatus", "0");
		List result = dao.query("ZMCHProduceReport.query", _map);
		if(result == null || result.isEmpty()) {

			return false;
		}
		else {
			return true;
		}
	}
}